import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class dyb {
    public static String datas;

    static void scrp(String count) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.zjsfgkw.cn/Notice/NoticeKTSearch");
        httpPost.addHeader("Host", "www.zjsfgkw.cn"); //认证token
        httpPost.addHeader("Origin", "http://www.zjsfgkw.cn");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36");
        // 设置2个post参数，一个是scope、一个是q
        List nameValuePairs = new ArrayList(2);
        nameValuePairs.add(new BasicNameValuePair("pageno", count));
        nameValuePairs.add(new BasicNameValuePair("pagesize", "10"));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                datas = content;
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    }

    public static void main(String[] args) throws Exception {
        int i;
        JSONObject jsondata = new JSONObject();
        Mongo db = new Mongo("218.77.58.165",27018,"test","dYb","test" ,"test");
        for (i = 1; i <= 2; i++) {
            scrp("" + i);
            jsondata = JSONObject.parseObject(datas);
            JSONArray array=jsondata.getJSONArray("list");
            for (int j =0; j <array.size(); j++) {
                //替换AJBS为_id
                JSONObject object = array.getJSONObject(j).fluentPut("_id",array.getJSONObject(j).getString("AJBS"));
                db.insertOne(object.getString("AJBS"), object);

            }
        }
        db.close();
    }
}

